<script setup>
import { ref, reactive } from 'vue'
import { useAuth } from '@/use/useAuth'

import { defineEmits } from 'vue'

const emits = defineEmits(['upUserInfo'])
import { getCurrentInstance } from 'vue'
const instance = getCurrentInstance()
let { proxy } = instance
const activeName = ref('second')
const username = ref('')
const password = ref('')
const ruleFormRef = ref()
const ruleForm = reactive({
  account: '',
  password: '',
  oncePassword: '',
})

const rules = reactive({
  account: [
    { required: true, message: proxy.$t('loginbox.enteraccount'), trigger: 'blur' },
    { min: 3, max: 16, message: proxy.$t('Maximumcharacters'), trigger: 'blur' },
  ],
  password: [
    { required: true, message: proxy.$t('loginbox.enterpassword'), trigger: 'blur' },
    { min: 6, max: 16, message: proxy.$t('Maximumcharacters'), trigger: 'blur' },
  ],
  oncePassword: [
    { required: true, message: proxy.$t('loginbox.enterpassword'), trigger: 'blur' },
    { min: 6, max: 16, message: proxy.$t('Maximumcharacters'), trigger: 'blur' },
  ],
})

const onClickLeft = () => history.back()
const { login, userRegister, user, userLogin } = useAuth()
const onRegister = async () => {
  if (ruleForm.password !== ruleForm.oncePassword) {
    ElMessage({
      message: '两次密码不一样,请重新输入',
      type: 'warning',
    })
    return
  }

  let data = await userRegister({
    username: ruleForm.account,
    password: ruleForm.password,
  })
  emits('upUserInfo', data)
  console.log(data)
  console.log(user.value)
  // onClickLeft()
}

const onLogin = async () => {
  let data = await userLogin({
    username: ruleForm.account,
    password: ruleForm.password,
  })
  emits('upUserInfo', data)
  console.log(data)
  console.log(user.value)
  // onClickLeft()
}

const handleClick = (tab, event) => {
  ruleForm.password = ''
  ruleForm.oncePassword = ''
  resetForm(ruleFormRef.value)
  console.log(tab, event)
}

const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      if (activeName.value === 'first') {
        onRegister()
      }
      if (activeName.value === 'second') {
        onLogin()
      }
      console.log('submit!')
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>
<template>
  <div class="login-box">
    <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
      <el-tab-pane :label="$t('loginbox.Signin')" name="first">
        <div class="input-box">
          <!-- <el-input v-model="input" style="width: 240px" placeholder="請輸入郵箱/賬號" /> -->

          <div>
            <el-form
              ref="ruleFormRef"
              style="max-width: 600px"
              :model="ruleForm"
              :rules="rules"
              label-width="auto"
              class="demo-ruleForm"
              status-icon
            >
              <el-form-item label="" prop="account">
                <el-input
                  v-model="ruleForm.account"
                  style="width: 260px"
                  :placeholder="$t('loginbox.enteraccount')"
                >
                  <template #prepend>
                    <el-icon><User /></el-icon>
                  </template>
                </el-input>
              </el-form-item>

              <el-form-item label="" prop="password">
                <el-input
                  v-model="ruleForm.password"
                  style="width: 260px"
                  :placeholder="$t('loginbox.enterpassword')"
                >
                  <template #prepend>
                    <el-icon><User /></el-icon>
                  </template>
                </el-input>
              </el-form-item>

              <el-form-item label="" prop="oncePassword">
                <el-input
                  v-model="ruleForm.oncePassword"
                  style="width: 260px"
                  :placeholder="$t('loginbox.enterpasswordagain')"
                >
                  <template #prepend>
                    <el-icon><User /></el-icon>
                  </template>
                </el-input>
              </el-form-item>
            </el-form>
          </div>

          <div class="login-box-btn">
            <div></div>
            <el-button type="primary" @click="submitForm(ruleFormRef)">{{
              $t('loginbox.Registernow')
            }}</el-button>
            <!-- <el-button type="primary">游客登入</el-button> -->
          </div>
          <div class="divider">
            <el-divider>
              <div>
                <el-avatar
                  :size="36"
                  src="https://cdn.91easyplay.com/public/pc/images/Pc_fb.png"
                  style="margin-right: 10px"
                />
                <el-avatar
                  :size="36"
                  src="https://cdn.91easyplay.com/public/pc/images/Pc_yahoo.png"
                />
              </div>
            </el-divider>
          </div></div
      ></el-tab-pane>
      <el-tab-pane :label="$t('loginbox.Login')" name="second">
        <div class="input-box">
          <!-- <el-input v-model="input" style="width: 240px" placeholder="請輸入郵箱/賬號" /> -->
          <el-form
            ref="ruleFormRef"
            style="max-width: 600px"
            :model="ruleForm"
            :rules="rules"
            label-width="auto"
            class="demo-ruleForm"
            status-icon
          >
            <el-form-item label="" prop="account">
              <el-input
                v-model="ruleForm.account"
                style="width: 260px"
                :placeholder="$t('loginbox.enteraccount')"
              >
                <template #prepend>
                  <el-icon><User /></el-icon>
                </template>
              </el-input>
            </el-form-item>

            <el-form-item label="" prop="password">
              <el-input
                v-model="ruleForm.password"
                style="width: 260px"
                :placeholder="$t('loginbox.enterpassword')"
              >
                <template #prepend>
                  <el-icon><User /></el-icon>
                </template>
              </el-input>
            </el-form-item>
          </el-form>
          <div class="login-box-btn">
            <!-- <el-button type="primary">游客登入</el-button> -->
            <span></span>
            <el-button type="primary" @click="submitForm(ruleFormRef)">{{
              $t('loginbox.Login')
            }}</el-button>
          </div>
        </div>
        <div class="divider">
          <el-divider>
            <div>
              <el-avatar
                :size="36"
                src="https://cdn.91easyplay.com/public/pc/images/Pc_fb.png"
                style="margin-right: 10px"
              />
              <el-avatar
                :size="36"
                src="https://cdn.91easyplay.com/public/pc/images/Pc_yahoo.png"
              />
            </div>
          </el-divider>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<style lang="scss" scoped>
.login-box {
  // background: #eeeded;
  display: flex;
  flex-direction: column;
  padding: 20px;

  .login-box-title {
    font-weight: bold;
    color: #3c3c3c;
    font-size: 20px;
  }
}
.input-box {
  margin-top: 10px;
}
.login-box-btn {
  display: flex;
  justify-content: space-between;
  margin-top: 16px;
  margin-bottom: 12px;
}
.divider {
  display: flex;
}
</style>
